package com.lezhi.socialgram.service.sys.impl;

import com.lezhi.socialgram.mapper.sys.SMSVerifyCodeMapper;
import com.lezhi.socialgram.model.sys.VerifySMSCode;
import com.lezhi.socialgram.service.sys.SMSVeriService;
import com.lezhi.socialgram.util.Param;
import com.lezhi.socialgram.util.TableUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.Date;
import java.util.List;

public class SMSMakeupThread extends Thread {
    private static Logger logger = LoggerFactory.getLogger(SMSMakeupThread.class);
    private String telNum;
    private SMSVerifyCodeMapper smsVerifyCodeMapper;
    private SMSVeriServiceImpl smsVeriService;
    private StringRedisTemplate stringRedisTemplate;
    private int veriGap;

    public SMSMakeupThread(String telNum, SMSVerifyCodeMapper smsVerifyCodeMapper, SMSVeriServiceImpl smsVeriService, int veriGap,
                           StringRedisTemplate stringRedisTemplate) {
        this.telNum = telNum;
        this.smsVerifyCodeMapper = smsVerifyCodeMapper;
        this.smsVeriService = smsVeriService;
        this.veriGap = veriGap;
        this.stringRedisTemplate = stringRedisTemplate;
    }

    public void run() {

        try {
            // 睡X秒

            Thread.sleep(veriGap * 1000);//休眠30秒后查找。
        } catch (InterruptedException e) {
        }

        if (smsVerifyCodeMapper != null && telNum != null) {

            try {
                //查找时间向前推35秒
                Date lastSendDate = new Date(new Date().getTime() - (veriGap + 10) * 1000);

                VerifySMSCode verifySMSCodeQO = new VerifySMSCode();
                verifySMSCodeQO.setTable(TableUtils.getTableNameWithId(VerifySMSCode.class, Long.parseLong(telNum), 10));
                verifySMSCodeQO.setTelNum(telNum);
                verifySMSCodeQO.setCreateTime(lastSendDate);
                List<VerifySMSCode> list = smsVerifyCodeMapper.getVerifySMSCodeList(verifySMSCodeQO);
                if (list != null && list.size() > 0) {
                    VerifySMSCode veriSMS = list.get(0);

                    //如果30秒后还没有验证成功，那么
                    if (veriSMS.getVerifyTime() == null && veriSMS.getStatus() == 1) {
                        int lastSendChannel = veriSMS.getChannel();

                        int channel = smsVeriService.getRandomSelectSMSChannel(lastSendChannel);

                        String veriCode = veriSMS.getVeriCode();
                        smsVeriService.sendVeriCodeViaSMS(telNum, veriCode, channel);

                        VerifySMSCode verifySMSCodeVO = new VerifySMSCode();
                        verifySMSCodeVO.setHisId(stringRedisTemplate.opsForValue().increment(Param.SMS_VERIFY_CODE_SEQUENCE, 1));
                        verifySMSCodeVO.setTable(TableUtils.getTableNameWithId(VerifySMSCode.class, Long.parseLong(telNum), 10));
                        verifySMSCodeVO.setTelNum(telNum);
                        verifySMSCodeVO.setVeriCode(veriCode);
                        verifySMSCodeVO.setStatus(VerifySMSCode.STATUS_MAKEUP);
                        verifySMSCodeVO.setChannel(channel);
                        smsVerifyCodeMapper.addVerifySMSCode(verifySMSCodeVO);
                    }
                }
            } catch (Exception e) {
                logger.error("makeup SMS error: ", e);
            }
        }

    }


    public String getTelNum() {
        return telNum;
    }


    public void setTelNum(String telNum) {
        this.telNum = telNum;
    }


    public SMSVerifyCodeMapper getSmsVerifyCodeMapper() {
        return smsVerifyCodeMapper;
    }

    public void setSmsVerifyCodeMapper(SMSVerifyCodeMapper smsVerifyCodeMapper) {
        this.smsVerifyCodeMapper = smsVerifyCodeMapper;
    }

    public SMSVeriServiceImpl getSmsVeriService() {
        return smsVeriService;
    }

    public void setSmsVeriService(SMSVeriServiceImpl smsVeriService) {
        this.smsVeriService = smsVeriService;
    }

    public int getVeriGap() {
        return veriGap;
    }

    public void setVeriGap(int veriGap) {
        this.veriGap = veriGap;
    }
}
